﻿using System;
using Microsoft.SharePoint;

namespace PS.Practices.Provisioner.Activation
{
    class FeatureActivationSink : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                 
                //var web = properties.UserCodeSite.RootWeb;
                var site = (SPSite)properties.Feature.Parent;
                var web = site.RootWeb;
                SPList spList = null;
                try
                {
                    string listUrl = web.ServerRelativeUrl == "/" ? "/_catalogs/provisionerscripts" : web.ServerRelativeUrl + "/_catalogs/provisionerscripts";
                    spList = web.GetList(listUrl);
                }
                catch (ArgumentException)
                {
                }

                if (spList != null)
                {
                    spList.BreakRoleInheritance(false);
                    spList.Update();
                }
            }
            catch(NullReferenceException)
            {
            }
        }
    }
}
